//
//  MenuViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "MenuViewController.h"
#import "AboutViewController.h"
#import "RemoveCache.h"
#import "SaveTools.h"
#import "AppDelegate.h"

typedef NS_ENUM(NSInteger, MenuBtnSelectIndex) {
    MenuBtnSelectIndexFirst = 5000,
    MenuBtnSelectIndexSeciond,
    MenuBtnSelectIndexThird
};

@interface MenuViewController ()

@property(nonatomic, assign) NSInteger number;

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myback"]];
  
    [self createView];
}

-(void)createView
{
#pragma mark 夜间模式
    NSString *string = [SaveTools readFromFileName:@"sun" andFilePath:NSLibraryDirectory];
    NSString *str;
    if ([string isEqualToString:@"0"]) {
        str = @"夜间模式";
    }
    else
    {
        str = @"日间模式";
    }
    
    NSArray *array = @[@"清除缓存",str, @"关于我们"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 400 * kHeight + i * kHeight * 35, self.view.frame.size.width - kWidth * 185, kHeight * 30);
        [button setTitle:array[i] forState:UIControlStateNormal];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        else
        {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        button.tag = 5000 + i;
        [self.view addSubview:button];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}

//点击事件
-(void)buttonAction:(UIButton *)button
{
    
    [UIView animateWithDuration:1 animations:^{
        
        button.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.0];
        
    } completion:^(BOOL finished) {
        
        button.backgroundColor = nil;
        
    }];
    
    //清理缓存
    if (button.tag == MenuBtnSelectIndexFirst) {
        
        NSArray *arr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *path = [arr firstObject];
        path = [path stringByAppendingString:@"/Caches"];
        RemoveCache *cache = [[RemoveCache alloc] init];
        CGFloat num = [cache floatWithPath:path];
        self.number = num;
        [cache removeCache];
        [self floadClear];
        [cache release];
    }
    else if (button.tag == MenuBtnSelectIndexSeciond)
    {
        UIWindow *view = [UIApplication sharedApplication].windows.firstObject;
        [view setBackgroundColor:[UIColor blackColor]];
        if ([button.titleLabel.text isEqual:@"夜间模式"]) {
            [view setAlpha:0.5];
            [button setTitle:@"日间模式" forState:UIControlStateNormal];
            [SaveTools saveToFileName:@"sun" andFilePath:NSLibraryDirectory andVaule:@"1"];
        }
        else
        {
            [view setAlpha:1];
            [button setTitle: @"夜间模式" forState:UIControlStateNormal];
            [SaveTools saveToFileName:@"sun" andFilePath:NSLibraryDirectory andVaule:@"0"];
        }
    }
    else
    {
        AboutViewController *about = [[AboutViewController alloc]init];
        about.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:about animated:YES completion:^{ }];
        [self.delegate sendApp];
        [about release];
    }
    
}

//显示清除的缓存大小
-(void)floadClear
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(45 * kWidth, 200 * kHeight, self.view.frame.size.width - kWidth * 270, kHeight * 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"清除%ldM缓存", self.number];
    label.backgroundColor = [[UIColor colorWithRed:1.000 green:0.577 blue:0.399 alpha:1.000] colorWithAlphaComponent:0.1];
    label.font = [UIFont systemFontOfSize:15 * kWidth];
    label.layer.cornerRadius = kWidth * 5;
    label.layer.masksToBounds = YES;
    label.layer.borderWidth = 1;
    label.layer.borderColor = [UIColor colorWithRed:0.992 green:0.784 blue:0.612 alpha:1].CGColor;
    [self.view addSubview:label];
    [label release];
    
    [UIView animateWithDuration:2 animations:^{
        
        label.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
